// Learn cc.Class:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const GameDataManger = require("../GameDataManger");
const UIPanelManager = require("../UIPanelManager");

cc.Class({
    extends: cc.Component,

    properties: {
        levelLab:{     
            default:null,
            type:cc.Label
        },

        desLab:{     
            default:null,
            type:cc.Label
        },

        costLab:{     
            default:null,
            type:cc.Label
        },

        page:{     
            default:null,
            type:cc.PageView
        },

        isPass:{     
            default:[],
            type:[cc.Node],
        },
    },

    onEnable(){
        this.curIndex = Math.min(2, GameDataManger.curLevel);

        this.page.setCurrentPageIndex(this.curIndex);

        for (let index = 0; index < this.isPass.length; index++) {
            const element = this.isPass[index];
            element.active = index < GameDataManger.curLevel;
        }

        this.refresh();
    },

    refresh(){

        this.levelLab.string = "第 " + (this.curIndex+1) + " 关";

        this.desLab.string = GameDataManger.levelConfig[this.curIndex].name;

        this.costLab.string = "消耗金币：5";
    },

    loadIamge(image, iconName){
        let url = 'imags/grades/' + iconName
        cc.resources.load(url, cc.SpriteFrame, (err, asset) => {
            if (err){
                console.log("资源加载失败")
                return;
            }
            image.getComponent(cc.Sprite).spriteFrame = asset;
        });
    },

    btnClick(event,str){
        if (str == "matchBtn"){
            //todo 开始游戏
            if (this.curIndex > GameDataManger.curLevel){
                cc.systemEvent.emit('showTip', "请先通过前面的关卡");
                return;
            }
            if (GameDataManger.gold < 5){
                cc.systemEvent.emit('showTip', "金币不足");
                return;
            }
            GameDataManger.gold-=5;
            GameDataManger.gameType = 2;
            var ranIndex = Math.floor(Math.random()*2);
            cc.systemEvent.emit('sendMessage', 1002, [0,true]);
            cc.systemEvent.emit('saveUserData');
            cc.systemEvent.emit('connectAi', this.curIndex);
            UIPanelManager.close(this.node);
        }else if (str == "left"){
            if (this.curIndex > 0){
                this.curIndex--;
                this.page.setCurrentPageIndex(this.curIndex);
                this.refresh();
            }
        }
        else if (str == "right"){
            if (this.curIndex < 2){
                this.curIndex++;
                this.page.setCurrentPageIndex(this.curIndex);
                this.refresh();
            }
        }
    },

    pageScroll(){
        this.curIndex =  this.page.getCurrentPageIndex();
        this.refresh();
    },


    onDisable(){
    },

    // update (dt) {},
});
